package com.kelvem.saas.workbanch.core.manager.controller;

import com.kelvem.saas.workbanch.core.base.Page;
import com.kelvem.saas.workbanch.core.exception.SaasAssert;
import com.kelvem.saas.workbanch.core.manager.model.MetaTableColumnEntity;
import com.kelvem.saas.workbanch.core.manager.request.MetaTableColumnQueryVo;
import com.kelvem.saas.workbanch.core.manager.service.MetaTableColumnService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 实体表字段控制层
 *
 * @author kelvem
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/metaTableColumn")
public class MetaTableColumnController {

    @Resource
    private MetaTableColumnService metaTableColumnService;
    
    /**
     * 增加实体表字段
     * curl -X POST http://localhost:9090/metaTableColumn/v1/add -H 'Content-Type: application/json' -d '{"id": null, "metaTableId": 3, "sortNo": 2, "enName": "en_name", "chName": "ch_name", "controlType": "control_type", "dbType": "db_type", "defaultValue": "default_value", "optionValue": "option_value", "requireFlag": 1, "span": 6, "width": 9, "sortFlag": 0, "showFlag": 1, "queryAble": 3, "addAble": 8, "editAble": 1, "sourceType": "source_type", "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo"}'
     *
     * @param metaTableColumn 实体表字段信息
     * @return Long 主键id
     */
    @PostMapping("/v1/add")
    public Long add(@RequestBody(required = false) MetaTableColumnEntity metaTableColumn) {
        SaasAssert.notNull(metaTableColumn);

        log.debug("MetaTableColumnController:add:start, metaTableColumn=【{}】", metaTableColumn);
        Long metaTableColumnId = metaTableColumnService.addMetaTableColumn(metaTableColumn);
        log.debug("MetaTableColumnController:add:end, metaTableColumnId={}", metaTableColumnId);

        return metaTableColumnId;
    }

    /**
     * 修改实体表字段
     * curl -X PUT http://localhost:9090/metaTableColumn/v1/update -H 'Content-Type: application/json' -d '{"id": 10000, "metaTableId": 3, "sortNo": 2, "enName": "en_name", "chName": "ch_name", "controlType": "control_type", "dbType": "db_type", "defaultValue": "default_value", "optionValue": "option_value", "requireFlag": 1, "span": 6, "width": 9, "sortFlag": 0, "showFlag": 1, "queryAble": 3, "addAble": 8, "editAble": 1, "sourceType": "source_type", "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo"}'
     *
     * @param metaTableColumn 实体表字段信息
     * @return Integer 修改记录数
     */
    @PostMapping("/v1/update")
    public Integer update(@RequestBody(required = false) MetaTableColumnEntity metaTableColumn) {
        SaasAssert.notNull(metaTableColumn);

        log.debug("MetaTableColumnController:update:start, metaTableColumn=【{}】", metaTableColumn);
        int count = metaTableColumnService.updateMetaTableColumn(metaTableColumn);
        log.debug("MetaTableColumnController:update:end, count={}", count);

        return count;
    }

    /**
     * 删除实体表字段
     * curl -X DELETE 'http://localhost:9090/metaTableColumn/v1/delete/10002'
     *
     * @param metaTableColumnId 实体表字段id
     * @return Integer 删除记录数
     */
    @PostMapping("/v1/delete/{metaTableColumnId}")
    public Integer delete(@PathVariable(required = false) Long metaTableColumnId) {
        SaasAssert.notNull(metaTableColumnId);

        log.debug("MetaTableColumnController:delete:start, metaTableColumnId={}", metaTableColumnId);
        int count = metaTableColumnService.deleteMetaTableColumn(metaTableColumnId);
        log.debug("MetaTableColumnController:delete:end, count={}", count);

        return count;
    }

    /**
     * 查询实体表字段分页数据
     * curl -X POST http://localhost:9090/metaTableColumn/v1/queryPage -H 'Content-Type: application/json' -d '{"id": 10000}'
     *
     * @param metaTableColumnQueryVo 查询用
     * @return Page<MetaTableColumnEntity> 实体表字段分页数据
     */
    @PostMapping("/v1/queryPage")
    public Page<MetaTableColumnEntity> queryPage(@RequestBody(required = false) MetaTableColumnQueryVo metaTableColumnQueryVo) {
        SaasAssert.notNull(metaTableColumnQueryVo);

        log.debug("MetaTableColumnController:queryPage:start, metaTableColumnQueryVo=【{}】", metaTableColumnQueryVo);
        Page<MetaTableColumnEntity> page = metaTableColumnService.queryMetaTableColumnPage(metaTableColumnQueryVo);
        log.debug("MetaTableColumnController:queryPage:end, page.count={}", page.getSize());

        return page;
    }

    /**
     * 根据id查实体表字段信息
     * curl -X GET 'http://localhost:9090/metaTableColumn/v1/get/10000'
     *
     * @param metaTableColumnId 实体表字段id
     * @return MetaTableColumn 实体表字段信息
     */
    @GetMapping("/v1/get/{metaTableColumnId}")
    public MetaTableColumnEntity getById(@PathVariable(required = false) Long metaTableColumnId) {
        SaasAssert.notNull(metaTableColumnId);

        log.debug("MetaTableColumnController:getById:start, metaTableColumnId={}", metaTableColumnId);
        MetaTableColumnEntity metaTableColumn = metaTableColumnService.getMetaTableColumnById(metaTableColumnId);
        log.debug("MetaTableColumnController:getById:end, metaTableColumn=【{}】", metaTableColumn);

        return metaTableColumn;
    }

    /**
     * 查询实体表字段列表数据
     * curl -X POST http://localhost:9090/metaTableColumn/v1/queryList -H 'Content-Type: application/json' -d '{"id": 10000}'
     *
     * @param metaTableColumnQueryVo 查询用
     * @return List<MetaTableColumnEntity> 实体表字段分页数据
     */
    @PostMapping("/v1/queryList")
    public List<MetaTableColumnEntity> queryList(@RequestBody(required = false) MetaTableColumnQueryVo metaTableColumnQueryVo) {
        SaasAssert.notNull(metaTableColumnQueryVo);

        log.debug("MetaTableColumnController:queryList:start, metaTableColumnQueryVo=【{}】", metaTableColumnQueryVo);
        List<MetaTableColumnEntity> list = metaTableColumnService.queryMetaTableColumnList(metaTableColumnQueryVo);
        log.debug("MetaTableColumnController:queryList:end, list.count={}", list.size());

        return list;
    }

    /**
     * 批量保存数据
     *
     * @param metaTableColumnList 批量数据
     * @return Integer 更新数据条数
     */
    @PostMapping("/v1/batch")
    public Integer batch(@RequestBody(required = false) List<MetaTableColumnEntity> metaTableColumnList) {
        SaasAssert.notNull(metaTableColumnList);

        log.debug("MetaTableColumnController:batch:start, metaTableColumnList.size={}", metaTableColumnList.size());
        int count = this.metaTableColumnService.batchMetaTableColumn(metaTableColumnList);
        log.debug("MetaTableColumnController:batch:end, opt.count={}", count);

        return count;
    }

}
